package com.kanshu.ksgb.fastread.doudou.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;

import butterknife.ButterKnife;
import butterknife.BindView;


/**
 * Created by wcy on 2017\6\8
 */

public class LoadingDialog extends Dialog {
    @BindView(R.id.loading_txt)
    TextView mMsgTv;
    CharSequence mMsg;
    Context mContext;

    public LoadingDialog(@NonNull Context context, CharSequence msg) {
        super(context, R.style.loading_dialog);
        mContext = context;
        mMsg = msg;
        init();
    }

    private void init() {
        setContentView(R.layout.loading_layout);
        ButterKnife.bind(this);
        mMsgTv.setText(mMsg);
        if (TextUtils.isEmpty(mMsg)) {
            DisplayUtils.gone(mMsgTv);
        }
    }

    @Override
    public void show() {
        if (mContext != null && mContext instanceof Activity && !((Activity)mContext).isFinishing()) {
            super.show();
        }
    }

    @Override
    public void dismiss() {
        try {
            if (mContext != null && mContext instanceof Activity && !((Activity)mContext).isFinishing()
                    && isShowing()) {
                super.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
